package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.bankInterface.IOrderMsg;
import com.fingard.dsp.bank.reqrespobj.ordermsg.ReqOrderMsgQry;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgQry;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02OrderMsg extends CITIC02Base implements IOrderMsg {
    @Override
    public RetAtsOrderMsgQry pushOrderMsg(ReqOrderMsgQry reqOrderMsgQry) throws Exception {
        RetAtsOrderMsgQry retAts = new RetAtsOrderMsgQry(reqOrderMsgQry.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCRODGM";

//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            NTCNO	核心企业订货计划编号	VARCHAR	20	Y
            xmlWriterBody.writeElementString("NTCNO",reqOrderMsgQry.DetailId);
//            CPCUSTCD	企业端经销商客户代码	VARCHAR	20	Y	上送经销商客户代码
            xmlWriterBody.writeElementString("CPCUSTCD",reqOrderMsgQry.DealerCode);
//            BKCUSTCD	银行端借款企业ID	VARCHAR	20	N
            xmlWriterBody.writeElementString("BKCUSTCD","");
//            LONCPNM	借款企业名称	VARCHAR	100	Y
            xmlWriterBody.writeElementString("LONCPNM",reqOrderMsgQry.DealerName);
//            OPBRHCD	经办分行编号	VARCHAR	10	N
            xmlWriterBody.writeElementString("OPBRHCD","");
//            OPBRHNM	经办分行名称	VARCHAR	100	N
            xmlWriterBody.writeElementString("OPBRHNM","");
//            CREDITNO	额度编号	VARCHAR	20	N	必输修改为非必输
            xmlWriterBody.writeElementString("CREDITNO","");
//            BRANDCD	品牌编码	VARCHAR	20	N
            xmlWriterBody.writeElementString("BRANDCD","");
//            BRANDNM	品牌名称	VARCHAR	100	N
            xmlWriterBody.writeElementString("BRANDNM","");
//            LOANTYPP	融资类型	VARCHAR	2	N	10 纸票 20流贷30电子票
            xmlWriterBody.writeElementString("LOANTYPP","");
//            LOANAMT	融资申请金额	DECIMAL	19,2	N
            xmlWriterBody.writeElementString("LOANAMT","");
//            IDTQTY	订货数量	DECIMAL	19,2	Y
            xmlWriterBody.writeElementString("IDTQTY",reqOrderMsgQry.OrderNum);
//            IDTTOTAMT	订货总价值	DECIMAL	19,2	Y
            xmlWriterBody.writeElementString("IDTTOTAMT",reqOrderMsgQry.OrderTotallAmount);
//            FIELD1	属性1	VARCHAR	30	N	交易流水号，必输
            // TODO: 2019/9/29
            xmlWriterBody.writeElementString("FIELD1",reqOrderMsgQry.DetailId);
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	N	1：新增2：撤销；若不输入默认为新增；若为撤销操作，则只有“核心企业订货计划编号“必输，其余字段非必输
            xmlWriterBody.writeElementString("FIELD3","");
//            LIST	汽车列表	LIST
            xmlWriterBody.writeStartElement("LIST");

            while (reqOrderMsgQry.readRecord()) {
                xmlWriterBody.writeStartElement("row");
//            PLDCODE	押品代码	VARCHAR	30	N
                xmlWriterBody.writeElementString("PLDCODE", "");
//            PLDNAME	押品名称	VARCHAR	100	Y	品牌+车型+配置
                // TODO: 2019/9/29
                xmlWriterBody.writeElementString("PLDNAME", reqOrderMsgQry.ReqDetail.CarTypeCode);
//            PLDPRICE	押品单价	DECIMAL	19,2	Y
                xmlWriterBody.writeElementString("PLDPRICE", reqOrderMsgQry.ReqDetail.PickupPrice);
//            PLDQTY	押品数量	DECIMAL	19,2	Y
                xmlWriterBody.writeElementString("PLDQTY", reqOrderMsgQry.ReqDetail.OrderNum);
//            PLDVALUE	押品货值	DECIMAL	19,2	N
                xmlWriterBody.writeElementString("PLDVALUE", "");
//            PLDBDCD	押品品牌代码	VARCHAR	60	N
                xmlWriterBody.writeElementString("PLDBDCD", "");
//            PLDBDNM	押品品牌名称	VARCHAR	100	N
                xmlWriterBody.writeElementString("PLDBDNM", "");
//            FIELD4	属性4	VARCHAR	30	N	汽车排量
                xmlWriterBody.writeElementString("FIELD4", "");
//            FIELD5	车架号	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD5", "");
//            FIELD6	属性6	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD6", "");
//            FIELD7	属性7	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD7", "");
//            FIELD8	属性8	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD8", "");
//            FIELD9	属性9	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD9", "");
//            FIELD10	属性10	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD10", "");
//            FIELD11	属性11	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD11", "");
//            FIELD12	属性12	VARCHAR	200	N
                xmlWriterBody.writeElementString("FIELD12", "");
                xmlWriterBody.writeEndElement();
            }
//                    LIST_END
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
//                    <?xml version="1.0" encoding="UTF-8"?>
//                    <stream>
//                    <RETCODE>AAAAAAA</RETCODE>
//                    </stream>
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        retAts.respCode = "0";
                    }else{
                        retAts.respCode = "-1";
                    }
                    retAts.respInfo = retCode;
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
